package com.bfxy.base.rabbit.common.convert;


import com.google.common.base.Preconditions;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;


/**
 * 装饰者，丰富属性
 * 	$RabbitMessageConverter
 * @author linxin
 *
 */
public class RabbitMessageConverter implements MessageConverter {

	// spring的message
	private GenericMessageConverter delegate;
	public RabbitMessageConverter(GenericMessageConverter gmc) {
		Preconditions.checkNotNull(gmc);
		this.delegate = gmc;
	}

	@Override
	public Object fromMessage(Message message) throws MessageConversionException {
		return (com.bfxy.base.rabbit.api.Message)this.delegate.fromMessage(message);
	}

	@Override
	public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException {
		com.bfxy.base.rabbit.api.Message message = (com.bfxy.base.rabbit.api.Message)o;
		// 装饰额外的属性
		messageProperties.setDelay(message.getDelayMills());
		return this.delegate.toMessage(o, messageProperties);
	}




}
